package com.gk.study.controller;

import com.alibaba.druid.util.StringUtils;
import com.gk.study.common.APIResponse;
import com.gk.study.common.ResponeCode;
import com.gk.study.common.Result;
import com.gk.study.entity.User;
import com.gk.study.service.UserService;
import com.gk.study.utils.JwtUtil;
import com.gk.study.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.mybatis.logging.Logger;
import org.mybatis.logging.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Pattern;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/user")
@CrossOrigin(origins = {"*"})
public class UserController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    UserService userService;

    /**
     * 登录操作
     * @param  // user
     * @return
     */
    // 后台用户登录
    @PostMapping("/login")
    public APIResponse login(@RequestParam("username") String username,@RequestParam("password") String password){
        log.info("开始登录，参数为{}",username);
        User responseUser = userService.getUserByUserName(username);
        if(responseUser == null) {
            System.out.println(responseUser);
            return new APIResponse(ResponeCode.FAIL, "用户名不存在");
        }
        if(!responseUser.getPassword().equals(password)) {
            System.out.println(responseUser);
            return new APIResponse(ResponeCode.FAIL, "密码错误");
        }
        if(responseUser.getStatus()==1){
            System.out.println(responseUser);
            return new APIResponse(ResponeCode.FAIL, "用户已经被禁了");
        }
        ThreadLocalUtil.set(username);
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", responseUser);
    }
//    @RequestMapping(value="/login",method = {RequestMethod.POST})
//    public APIResponse login(@Pattern(regexp = "^[a-zA-Z0-9]{5,16}$") String username, @Pattern(regexp ="^[a-zA-Z0-9]{5,16}$") String password){
//        User user = userService.findByUserName(username);
//        if (user == null){
//            return new APIResponse<>(ResponeCode.FAIL,"用户错误");
//        }else if ( user.getPassword().equals(password)){
//            Map<String,Object> claims = new HashMap<>();
//            claims.put("id",user.getId());
//            claims.put("username",user.getUsername()) ;
//            String token = JwtUtil.getToken(claims);
//            System.out.println("---------------------------");
//            System.out.println(token);
//            ValueOperations<String, String> value = stringRedisTemplate.opsForValue();
//            value.set(token,token,1, TimeUnit.HOURS);
//            return new APIResponse<>(ResponeCode.SUCCESS,token);
//        }else {
//            return new APIResponse(ResponeCode.FAIL);
//        }
//    }
    /**
     * 查询用户信息
     * @param keyword
     * @return
     */
    @GetMapping(value = "/list")
    public APIResponse list(String keyword){
        log.info("开始查询用户信息,参数为{}",keyword);
        List<User> list =  userService.getUserList(keyword);
        return new APIResponse(ResponeCode.SUCCESS, "查询成功", list);
    }

    /**
     * 新增操作
     * @param user
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/create")
    @Transactional
    public APIResponse create(User user) throws IOException {

        if (!StringUtils.isEmpty(user.getUsername()) || !StringUtils.isEmpty(user.getPassword())) {
            // 查重
            if(userService.getUserByUserName(user.getUsername()) != null) {
                return new APIResponse(ResponeCode.FAIL, "用户名重复");
            }

            user.setCreateTime(String.valueOf(LocalDateTime.now()));
            userService.createUser(user);
            return new APIResponse(ResponeCode.SUCCESS, "创建成功");
        }
        return new APIResponse(ResponeCode.FAIL, "创建失败");
    }

    /**
     * 删除用户信息操作
     * @param ids
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public APIResponse delete(String ids){
        System.out.println("ids===" + ids);
        // 批量删除
        String[] arr = ids.split(",");
        for (String id : arr) {
            userService.deleteUser(id);
        }
        return new APIResponse(ResponeCode.SUCCESS, "删除成功");
    }

    /**
     * 更新用户信息操作
     * @param user
     * @return
     * @throws IOException
     */
//    @Access(level = AccessLevel.ADMIN)
    @PostMapping(value = "/update")
    @Transactional
    public APIResponse update(User user) throws IOException {
        // update不能修改密码，故置空
        user.setPassword(null);

        userService.updateUser(user);
        System.out.println(user);
        return new APIResponse(ResponeCode.SUCCESS, "更新成功");
    }
    
}
